package com.hrbxlh.util;

/**
  * 文件说明
  * @Description:扩展说明
  * @Copyright: 2015 dreamtech.com.cn Inc. All right reserved
  * @Version: V6.0
  */

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.vo.SMS;
import com.hrbxlh.vo.SMSVO;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;


public class SendMsgUtil {
	
	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("param1", "李辛");
		map.put("param2", "3");
		map.put("state", 2);
		//System.out.println(SendMsgUtil.sendSMSInfo("13114509532", map));
	}
	
	public static String sendSMSInfo(String phones,Map<String,Object> map){
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		ObjectMapper mapper = new ObjectMapper();
		Date time = new Date();
		String url = "";
		String sig = "";
		String timestamp = "";
		String reContent = "";
		
		try {
			
			// 从配置文件中获取信息
			String[] keysArray = {"ACCOUNT_SID", "AUTH_TOKEN", "SMS_ADDRESS","TEMPLATE"};
			paramMap = ConfigFileUtil.getFileMessage("SMSPro.properties", keysArray);
			SMSVO SMSVO = mapper.readValue("{\"smsList\":"+(String)paramMap.get("TEMPLATE")+"}",
					com.hrbxlh.vo.SMSVO.class);
			
			//短信接口URL提交地址
			url = (String) paramMap.get("SMS_ADDRESS");
			
			for(SMS sms:SMSVO.getSmsList()){
				
				if(sms.getState() == (Integer)map.get("state")){
					
					for (Entry<String, Object> entry : map.entrySet()) {
						String key = entry.getKey().toString();
						sms.setContent(sms.getContent().replace("#{"+key+"}", map.get(key).toString()));
					}
					
					reContent = sms.getSignature()+sms.getContent();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			     	timestamp = sdf.format(time);
					sig = DigestUtils.md5Hex((String) paramMap.get("ACCOUNT_SID") + (String) paramMap.get("AUTH_TOKEN") + timestamp);
						
					break;
				}
				
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SendMsgUtil.postRequest(url, "accountSid="+(String) paramMap.get("ACCOUNT_SID")+"=" + reContent + "&to=" + phones + "&timestamp=" + timestamp + "&sig=" + sig);
		//return reContent;
	}
	
	/**
	 * 发送短信消息
	  * 方法说明
	  * @param phones
	  * @param content
	  * @return String
	 */
	public static String sendMsg(String phones,Map<String,Object> map){
		
		//短信接口URL提交地址
		String url = "https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
		String reContent = "【医大二院】"+(String)map.get("param1")+"您好，距离您的下次随访时间还有"+(String)map.get("param2")+"天";
		Date time = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
     	String timestamp = sdf.format(time);
		String sig = DigestUtils.md5Hex("6f53264d05bc43bcafc1a47b30fdd296" + "2a9afc0b29e842339172790584e6e40d" + timestamp);
				
		return SendMsgUtil.postRequest(url, "accountSid=6f53264d05bc43bcafc1a47b30fdd296&smsContent=" + reContent + "&to=" + phones + "&timestamp=" + timestamp + "&sig=" + sig);
	}
	
	
	
	/**
	 * 发送短信消息
	  * 方法说明
	  * @param phones
	  * @param content
	  * @return String
	 */
	public static String sendMsgYZ(String phones,String content){
		
		//短信接口URL提交地址
		String url = "https://api.miaodiyun.com/20150822/industrySMS/sendSMS";

		String reContent = "【大数据验证】您的验证码为"+content+"，请于5分钟内正确输入验证码";
		//System.out.println(reContent);
		Date time = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
     		String timestamp = sdf.format(time);
		String sig = DigestUtils.md5Hex("6f53264d05bc43bcafc1a47b30fdd296" + "2a9afc0b29e842339172790584e6e40d" + timestamp);
				
		return SendMsgUtil.postRequest(url, "accountSid=6f53264d05bc43bcafc1a47b30fdd296&smsContent=" + reContent + "&to=" + phones + "&timestamp=" + timestamp + "&sig=" + sig);
	}
	
	/**
	 * 随机生成6位随机验证码
	 */
	public static String createRandomVcode(){
		//验证码
		String vcode = "";
		for (int i = 0; i < 6; i++) {
			vcode = vcode + (int)(Math.random() * 9);
		}
		return vcode;
	}
	
	public static String postRequest(String url, String body) {

		System.out.println(url+"."+body);
		String result = "";
		try
		{
			OutputStreamWriter out = null;
			BufferedReader in = null;
			URL realUrl = new URL(url);
			URLConnection conn = realUrl.openConnection();
	
			// 设置连接参数
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setConnectTimeout(5000);
			conn.setReadTimeout(20000);
	
			// 提交数据
			out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
			out.write(body);
			out.flush();
	
			// 读取返回数据
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
			String line = "";
			boolean firstLine = true; // 读第一行不加换行符
			while ((line = in.readLine()) != null)
			{
				if (firstLine)
				{
					firstLine = false;
				} else
				{
	//				result += System.lineSeparator();
				}
				result += line;
			}
		
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return result;
		
	}


	
}
